package com.kele.controller;

import com.kele.domain.Blog;
import com.kele.domain.Sentence;
import com.kele.service.SentenceService;
import com.kele.utils.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 赵奥林
 * @date 2021年06月05日 15:04
 * @Description
 */

@Controller
public class SentenceController {

    @Resource
    private SentenceService sentenceService;

    /**
     * 查询所有的句子
     * @return
     */
//    @RequestMapping(value = "findSentence",method = RequestMethod.GET)
//    @ResponseBody
//    public ResponseData<Sentence> findSentence(Model model){
//        List<Sentence> sentence = sentenceService.findSentence();
//        model.addAttribute("sentence",sentence);
//        return ResponseData.success("查询成功",sentence);
//    }

    /**
     * 随机抽取句子展示在页面上
     */

    @RequestMapping(value = "showSentence",method = RequestMethod.GET)
    public String showSentence(Model model) {
        List<Sentence> sentences = sentenceService.showSentence();
        List<Sentence> list = new ArrayList<Sentence>();
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            int intRandom = random.nextInt(sentences.size() - 1);
            list.add(sentences.get(intRandom));
            sentences.remove(sentences.get(intRandom));
        }
        model.addAttribute("sentence",sentences);
        return "static_resources/templates/index";

    }

}
